package com.lanou.dllo.vc.activity;

import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.lanou.dllo.vc.MyApplication;
import com.lanou.dllo.vc.R;
import com.lanou.dllo.vc.dao.UserInfoDBhelper;
import com.lanou.dllo.vc.greendao.UserInfo;
import com.lanou.dllo.vc.sdkhelpter.HXSDKHelper;
import com.lanou.dllo.vc.volley.DemoHXSDKHelper;
import com.lanou.dllo.vc.volley.DiskCache;

import java.io.ByteArrayOutputStream;

import butterknife.Bind;
import butterknife.ButterKnife;

public class SettingUserActivty extends BaseActivity implements View.OnClickListener {
    @Bind(R.id.user_header)
    public ImageView user_header;//用户头像
    private DiskCache diskCache;
    private UserInfoDBhelper helper;
    @Bind(R.id.username)
    public TextView user;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


    }

    @Override
    protected int getLayout() {
        return R.layout.activity_setting_user_activty;
    }

    @Override
    protected void initView() {
        ButterKnife.bind(this);
        user_header.setOnClickListener(this);
        diskCache = new DiskCache();
        helper = new UserInfoDBhelper();


    }

    @Override
    protected void initData() {
//            UserInfo userInfo = helper.readDate(MyApplication.getInstance().getUserName());
//        String url = userInfo.getImgurl();
//        Log.i("lanou", url);
//        if (url != null) {
//            user_header.setImageBitmap(diskCache.getBitmap(url));
//        }
        user.setText(MyApplication.getUserName());

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.user_header:
                //系统图片
                Intent intent = new Intent(Intent.ACTION_PICK, null);
                intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");
                startActivityForResult(intent, 12);


                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case 12:
                if (data != null) {

                    getImageView(data.getData());
                }

                break;
            case 13:
                if (data != null) {
                    //剪裁图片
                    setPicToView(data);
                }
                break;
            default:
                break;
        }

    }

    public void getImageView(Uri uri) {
        Intent intent = new Intent("com.android.camera.action.CROP");
        intent.setDataAndType(uri, "image/*");
        intent.putExtra("crop", true);
        intent.putExtra("aspectX", 1);
        intent.putExtra("aspectY", 1);
        intent.putExtra("outputX", 300);
        intent.putExtra("outputY", 300);
        intent.putExtra("return-data", true);
        intent.putExtra("noFaceDetection", true);
        startActivityForResult(intent, 13);
    }

    private void setPicToView(Intent picdata) {
        Bundle extras = picdata.getExtras();
        if (extras != null) {
            Bitmap photo = extras.getParcelable("data");
            // Drawable drawable = new BitmapDrawable(getResources(), photo);
            user_header.setImageBitmap(photo);
            uploadUserAvatar(Bitmap2Bytes(photo));
            //  diskCache.putBitmap("headimg",photo);

        }

    }

    private ProgressDialog dialog;

    private void uploadUserAvatar(final byte[] data) {
        dialog = ProgressDialog.show(this, "正在上传头像", "请等待");
        new Thread(new Runnable() {

            @Override
            public void run() {
                final String avatarUrl = ((DemoHXSDKHelper) HXSDKHelper.getInstance()).getUserProfileManager().uploadUserAvatar(data);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {

                        dialog.dismiss();
                        if (avatarUrl != null) {
                            Toast.makeText(SettingUserActivty.this, "上传成功",
                                    Toast.LENGTH_SHORT).show();
                        } else {
                            Toast.makeText(SettingUserActivty.this, "上传失败",
                                    Toast.LENGTH_SHORT).show();
                        }

                    }
                });

            }

        }).start();

        dialog.show();
    }


    public byte[] Bitmap2Bytes(Bitmap bm) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        bm.compress(Bitmap.CompressFormat.PNG, 100, baos);
        return baos.toByteArray();
    }

    public void back(View view) {
//        UserInfo info = helper.readDate(MyApplication.getInstance().getUserName());
//        info.setImgurl("headimg");
//        helper.saveData(info);
//        Log.d(TAG, "headimg");
        finish();

    }
}
